package cert.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.cert.X509Certificate;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import cert.dal.Crud;
import cert.model.CertAuthority;
import cert.utils.CertGen;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class ConfigureCADialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtCa;
	private JTextField textField_validity;

	/**
	 * Create the dialog.
	 */
	public ConfigureCADialog() {
		setModal(true);

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		setTitle("Cr\u00E9er le certificat de l'autorit\u00E9 de certification");
		setBounds(100, 100, 450, 250);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(new FormLayout(new ColumnSpec[] {
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("default:grow"), }, new RowSpec[] {
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC, }));
			{
				JLabel lblClPriv = new JLabel("Cl\u00E9 priv\u00E9e : ");
				panel.add(lblClPriv, "2, 2");
			}
			{
				JButton btnCrerUneNouvelle = new JButton(
						"Cr\u00E9er une nouvelle cl\u00E9...");
				panel.add(btnCrerUneNouvelle, "4, 2");
			}
			{
				JLabel lblNomDeLautorit = new JLabel(
						"Nom de l'autorit\u00E9 de certification (CN)");
				panel.add(lblNomDeLautorit, "2, 4, right, default");
			}
			{
				txtCa = new JTextField();
				txtCa.setText("CA");
				panel.add(txtCa, "4, 4, fill, default");
				txtCa.setColumns(10);
			}
			{
				JLabel lblPriodeDeValidit = new JLabel(
						"P\u00E9riode de validit\u00E9 (Ann\u00E9es)");
				panel.add(lblPriodeDeValidit, "2, 6, right, default");
			}
			{
				textField_validity = new JTextField();
				textField_validity.setText("5");
				panel.add(textField_validity, "4, 6, fill, default");
				textField_validity.setColumns(10);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {

						String caName = txtCa.getText();
						int validity = Integer.parseInt(textField_validity
								.getText());

						try {

							X509Certificate cert = CertGen.generateCertificate(
									caName, caName, CertAuthority.getKeyPair(),
									validity * 365, "sha1withRSA");

							// save certificate
							Crud crud = new Crud();
							crud.addCaCertificate(caName,
									CertGen.getString(cert));

							ConfigureCADialog.this.dispose();

						} catch (Exception e) {
							JOptionPane.showMessageDialog(
									ConfigureCADialog.this, e.toString());
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						ConfigureCADialog.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}

}
